package day11;

import java.util.concurrent.locks.ReentrantLock;

public
class Deadlock2 {
    ReentrantLock lock1 = new ReentrantLock();
    ReentrantLock lock2 = new ReentrantLock();

    public static
    void main(String[] args) {
        Deadlock2 deadlock2 = new Deadlock2();
        new Thread(() -> {
            deadlock2.test();
        }, "线程1").start();
        new Thread(() -> {
            deadlock2.test2();
        }, "线程2").start();
    }

    public
    void test() {
        String name = Thread.currentThread().getName();
        lock1.lock();
        System.out.println(name + "获得lock1锁");
        lock2.lock();
        System.out.println(name + "获得lock2锁");
        lock2.unlock();
        System.out.println(name + "释放lock2锁");
        lock1.unlock();
        System.out.println(name + "释放lock1锁");
    }

    public
    void test2() {
        String name = Thread.currentThread().getName();
        lock2.lock();
        System.out.println(name + "获得lock2锁");
        lock1.lock();
        System.out.println(name + "获得lock1锁");
        lock1.unlock();
        System.out.println(name + "释放lock1锁");
        lock2.unlock();
    }
}
